package treediff.parameters;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.io.FileUtils;

public class NumberOfComparisons {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		
		Map<String,Map<String,Integer>> map = new HashMap<String, Map<String,Integer>>();		
		
		String[] updates = {"added","changed","removed","total"};
		
		String type = "training";
		
		Set<String> toProcess = loadToProcess(new File("/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/" + type));
		
		File input = new File("/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/BreakDetectorReloaded/" + type + "/");
		
		File[] folders = input.listFiles();
		
		Arrays.sort(folders,new Comparator<File>(){

			@Override
			public int compare(File o1, File o2) {
				
				return Double.compare(Double.valueOf(o1.getName()), Double.valueOf(o2.getName()));
				
			}
			
		});

		initialize(map,toProcess,updates);
		
		for (String db : toProcess) {
			
			System.out.println("DATABASE: " + db);
			
			File prevfolder = new File(folders[0],db);
			
			for (int i = 1; i < folders.length; i++) {
				
				File thisFolder = new File(folders[i],db);
				
				for (int up = 0; up < updates.length; up++) {
				
					String name = db + "." + updates[up] + ".break";
					
					File prev = new File(prevfolder,name);
					
					File after = new File(thisFolder,name);
					
					if (!prev.exists() || !after.exists())
						continue;
					
					if (!isEquals(prev,after)){
					
						map.get(db).put(updates[up], i);
						
					}
					
				}
				
				prevfolder = thisFolder;
			}
			
		}		
		
		for (Entry<String,Map<String,Integer>> entry : map.entrySet()) {
			
			System.out.println(entry.getKey() + ": " + entry.getValue().toString());
			
		}
		
	}

	private static void initialize(Map<String, Map<String, Integer>> map,
			Set<String> dbs, String[] updates) {

		for (String db : dbs) {
			
			map.put(db, new HashMap<String, Integer>());
			
			for (String text : updates) {
				
				map.get(db).put(text, 0);
				
			}
		}
				
	}

	private static boolean isEquals(File file, File file2) {
		
		try {
		
			BufferedReader br = new BufferedReader(new FileReader(file));
			
			String level = br.readLine();
			
			String node = br.readLine();
			
			br.close();
			
			BufferedReader br2 = new BufferedReader(new FileReader(file2));
			
			String level2 = br2.readLine();
			
			String node2 = br2.readLine();
			
			br2.close();
			
			if (!level.equals(level2))
				return false;
			else{
				
				node = node.substring(node.indexOf(',')+1);
				
				node2 = node2.substring(node2.indexOf(',')+1);
				
				return node.equals(node2);
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}

	private static Set<String> loadToProcess(File file) throws IOException {
		
		Set<String> set = new HashSet<String>();
		
		BufferedReader br = new BufferedReader(new FileReader(file));
		
		String line;
		
		while((line=br.readLine())!=null){
			
			set.add(line);
			
		}
		
		br.close();
		
		return set;
		
	}
	
}
